<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */


class FormRender{
  
  private $form;

  /** @var object Html **/
  private $body;

  private $label;

  private $container;
  
  public function __construct(Form $form)
  {
    $this->form      = $form;
    $this->body      = Html::el('dl');
    $this->label     = Html::el('dt');
    $this->container = Html::el('dd');
  }
  
  
  /**
   * Render
   * @param string $type   
   * @return string   
   */    
  public function render($type = 'form')
  {
    if(method_exists($this, 'render'.$type))
       return call_user_method('render'.$type, $this);
    throw new Exception("Unknown part '".$type."'.");
  }
  
  
  
  /**
   * Render all part form
   * @return string   
   */    
  private function renderForm()
  {
    return $this->render('head').$this->render('body').$this->render('end');
  }
  
  
  
  /**
   * Render Start form
   * @return string   
   */    
  private function renderHead()
  {
    return $this->form->getPrototype()->startElement();
  }
  
  
  
  /**
   * Render Body form 
   * @return string   
   */ 
  private function renderBody()
  {
    if($this->form->autoRenderError()){
        foreach($this->form->getMessages() as $message){
              $this->body->setHtml( $this->form->getMessagePrototype()->setText($message)->render() );
              $this->form->getMessagePrototype()->cleanContent();
        }
    }
    foreach($this->form as $container){
        foreach($container->getRuleErrors() as $message){
            if($this->form->autoRenderError()){
               $this->body->setHtml( $container->getError()->setText($message)->render() );
               $container->getError()->cleanContent();
            }
        }

      if($container instanceof FormButtonExtends){
        $this->body->setHtml($this->container->setHtml($container->render())->render());
      }elseif($container instanceof FormCheckboxInput){
        $this->body->setHtml($this->label->setHtml(' '));
        $this->body->setHtml($this->label->setHtml($container->render().$container->getLabel())->render());
      }else{
        $this->body->setHtml($this->label->setHtml($container->getLabel())->render());
        $this->body->setHtml($this->container->setHtml($container->render())->render());
      }
      
      $this->label->cleanContent();
      $this->container->cleanContent();
      
    }
    return $this->body->render();
  }
  
  
  
  /**
   * Render End form 
   * @return string   
   */     
  private function renderEnd()
  {
    return $this->form->getPrototype()->endElement();
  }
}
?>